package com.swordget.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.swordget.service.SwitchingDataSourcesService;
import com.swordget.service.Sys_DataSourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

@Service
public class SwitchingDataSourcesServiceImpl implements SwitchingDataSourcesService {

    private final static Logger LOG = LoggerFactory.getLogger(SwitchingDataSourcesServiceImpl.class);

    @Autowired
    private DruidDataSource dataSource;


    @Autowired
    private Sys_DataSourceService sysDataSourceService;

    @Override
    public Connection switchingDataSources(String dataSourceId) {
        Connection conn = null;
        try {
            if ("0000000000000000000".equals(dataSourceId)) {
                conn = dataSource.getConnection();
            } else {
                conn = sysDataSourceService.createConnection(dataSourceId);
              /*  if (Objects.isNull(conn) || conn.isClosed()) {
                    conn = dataSource.getConnection();
                }*/
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }
}
